//
//  BaseTableViewController.swift
//  Sina
//
//  Created by 袁量 on 2016/11/29.
//  Copyright © 2016年 袁量. All rights reserved.
//

import UIKit

class BaseTableViewController: UITableViewController {

    var isLogIN = UserAccountViewModel.shareIntance.isLogin
    var visitorView: VisitorView?
    override func loadView() {

        // 判断用户是否登陆
        loadInfo() ? super.loadView() : setupVistorView()
    }
}

// MARK: - 设置 UI 界面
extension BaseTableViewController {
    
    func setupVistorView()
    {
        visitorView = VisitorView.visitorView()
        // otherView.backgroundColor = UIColor.green
        view = visitorView
        visitorView?.logoInButton.addTarget(self, action: #selector(BaseTableViewController.loginClick), for: .touchUpInside)
    }
    

}

// MARK: - 响应事件
extension BaseTableViewController {
    
    func loginClick() {
        let oauthVC = OAuthViewController()
        let oauthNav = UINavigationController(rootViewController: oauthVC)
        present(oauthNav, animated: true, completion: nil)
    }
}

// MARK: - 读取数据
extension BaseTableViewController {
    func loadInfo() -> (Bool) {
        var accountPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        accountPath = (accountPath as NSString).appendingPathComponent("account.plist")
        let account = NSKeyedUnarchiver.unarchiveObject(withFile: accountPath) as? UserAccount
        if account != nil && account?.expires_date?.compare(NSDate() as Date) == ComparisonResult.orderedDescending {
                return true
        }
        return false
    }
}
